package com.wsj.a.collapsingdemo.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by linxiujie on 2018/8/15.
 */

@SuppressLint("AppCompatCustomView")
public class FitImageView extends ImageView {
    public FitImageView(Context context) {
        super(context);
    }

    public FitImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FitImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable = getDrawable();
        if(drawable != null){
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) Math.ceil((float)width *(float) drawable.getIntrinsicHeight()/(float)drawable.getIntrinsicWidth());
            setMeasuredDimension(width,height);
        }else{
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }

    }
}
